package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.*;
import com.qf.service.ChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("charge")
public class ChargeController {
    @Autowired
    private ChargeService chargeService;

//    @RequestMapping("findAll")
//    public ModelAndView findAll() {
//        ModelAndView modelAndView = new ModelAndView();
//        List<ChargeItems> allChargeItems = chargeService.findAllChargeItems();
//        System.out.println(allChargeItems);
//        modelAndView.addObject(allChargeItems);
//        modelAndView.setViewName("/listcharges.html");
//        return modelAndView;
//    }
    // 分页查询  暂时缺多条件没有封装实体类
    @RequestMapping("findByPage")
    @ResponseBody
    public JSONResult findByPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                             @RequestParam(value = "pageSize",required = false,defaultValue = "2")Integer pageSize
                             ) {
        // 设置分页条件
        PageHelper.startPage(pageNum,pageSize);

        // 查询所有信息
        List<ChargeItems> all = chargeService.findAllChargeItems();

        //分页对象
        PageInfo<ChargeItems> pageInfo = new PageInfo<>(all);

        JSONResult jsonResult = new JSONResult();
        jsonResult.setCode(0);
        jsonResult.setCount(pageInfo.getTotal());
        jsonResult.setMsg("success");
        jsonResult.setData(all);

//        System.out.println(jsonResult);
        return jsonResult;
    }

     // 根据ID删除
    @RequestMapping("deleteById")
    @ResponseBody
    public String deleteById(Integer id) {


        int i = chargeService.deleteByPrimaryKey(id);

        if (i != 0) {
            return "success";
        }else {
            return null;
        }
    }

    @RequestMapping("edit")
    public String edit(Integer id,Model model) {

        ChargeItems chargeItems = chargeService.selectByPrimaryKey(id);

        List<Items> items = chargeService.findAllItems();
        List<Community> allCommunity = chargeService.findAllCommunity();

        model.addAttribute("chargeDetail",chargeItems);
        model.addAttribute("items",items);
        model.addAttribute("allCommunity",allCommunity);

        return "跳转到添加的页面";

    }


    // 添加或者修改
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(ChargeItems chargeItems,Integer id) {

        if (id != null) {
            chargeService.updateByPrimaryKey(chargeItems);
        } else {
            chargeService.insert(chargeItems);
        }

        return "redirect:/charge/findByPage";

    }
}
